package com.xr.log.receiver;

import com.alibaba.fastjson.JSONObject;
import com.xr.common.entity.ExceptionLog;
import com.xr.common.entity.OperationLog;
import com.xr.log.service.IExceptionLogService;
import com.xr.log.service.IOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class LogReceiver {

	@Resource
	private IOperationLogService operationLogService;

	@Resource
	private IExceptionLogService exceptionLogService;

	/**
	 * 接收操作日志数据，持久化到数据库
	 */
	@RabbitListener(queues = "operationLogQueue")
	public void receiveOperationLog(@Payload String operationLogJsonStr) {
		log.info("当前时间戳<{}>，LogReceiver接收消息<{}>", System.currentTimeMillis(), operationLogJsonStr);
		operationLogService.save(JSONObject.parseObject(operationLogJsonStr, OperationLog.class));
	}

	/**
	 * 接收异常日志数据，持久化到数据库
	 */
	@RabbitListener(queues = "exceptionLogQueue")
	public void receiveExceptionLog(@Payload String exceptionLogJsonStr) {
		log.info("当前时间戳<{}>，LogReceiver接收消息<{}>", System.currentTimeMillis(), exceptionLogJsonStr);
		exceptionLogService.save(JSONObject.parseObject(exceptionLogJsonStr, ExceptionLog.class));
	}

}
